package com.yanxin.credit.service;

import io.minio.MinioClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

@Slf4j
@Service
public class MinIoService {


    private MinioClient minioClient;

    public MinIoService(MinioClient minioClient) {
        this.minioClient = minioClient;
    }

    public InputStream getObject(String bucketName, String objectName) {
        try {
            return minioClient.getObject(bucketName, objectName);

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new IllegalStateException(e.getMessage());
        }
    }


    public void putObject(String bucketName, String objectName, String filename) {
        try {
            minioClient.putObject(bucketName, objectName, filename, null);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new IllegalStateException(e.getMessage());
        }
    }


    public void putObjectForContent(String bucketName, String objectName, String content) {
        try {
            InputStream inputStream = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
            putObject(bucketName, objectName, inputStream);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new IllegalStateException(e.getMessage());
        }
    }


    public void putObject(String bucketName, String objectName, InputStream stream) {
        try {
            minioClient.putObject(bucketName, objectName, stream, null);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new IllegalStateException(e.getMessage());
        }
    }


    public void putObject(String bucketName, String objectName, InputStream stream, String contentType) {
        try {
            minioClient.putObject(bucketName, objectName, stream, contentType);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new IllegalStateException(e.getMessage());
        }
    }


    public void removeObject(String bucketName, String objectName) {
        try {
            minioClient.removeObject(bucketName, objectName);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new IllegalStateException(e.getMessage());
        }
    }


    public String getObjectUrl(String bucketName, String objectName, Integer expires) {
        try {
            if (expires == 0) {
                // minio最大值 0会报错
                expires = 604800;
            }
            return minioClient.presignedGetObject(bucketName, objectName, expires);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new IllegalStateException(e.getMessage());
        }
    }

    public String getLocalObjectUrl(String bucketName, String objectName, Integer expires) {
        try {
            if (expires == 0) {
                //minio最大值 0会报错
                expires = 604800;
            }
            return minioClient.presignedGetObject(bucketName, objectName, expires);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new IllegalStateException(e.getMessage());
        }
    }

}
